package mq

import (
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
	"github.com/zeromicro/go-zero/core/logx"
)

// InitUniversalProducer 初始化普通生产者
// https://github.com/apache/rocketmq-client-go
func InitUniversalProducer() (rocketmq.Producer, error) {
	namesrvs := []string{"124.220.82.36:9876"}
	// 生产者组名称
	var groupName = "user-api-group"
	// 设置消息轨迹
	traceCfg := &primitive.TraceConfig{
		Access:   primitive.Local,
		Resolver: primitive.NewPassthroughResolver(namesrvs),
	}
	// 设置生产者
	p, err := rocketmq.NewProducer(
		producer.WithNsResolver(primitive.NewPassthroughResolver(namesrvs)),
		producer.WithGroupName(groupName),
		producer.WithRetry(2),
		producer.WithTrace(traceCfg),
	)

	if err != nil {
		logx.Errorf("new producer error: %s", err.Error())
		return nil, err
	}
	startErr := p.Start()
	if startErr != nil {
		logx.Errorf("start producer error: %s", startErr.Error())
		return nil, startErr
	}
	return p, nil
}
